function initPage (){ 
     var c = 0
     var cloned
     
     
     $("#addAdditionalFriendField").click(function() {
          $("#friendsToInvite").append("<tr id='newField'></tr>")
          cloned = $("#invite"+c);
          $("#newField").append("<td id='textcell'></td>")
          $("#textcell").append($("#invite"+c).clone().attr('id','invite'+ (++c)).attr('name','invite'+(c)))
          $("#newField").append("<td id='buttoncell'><td>")
          $("#addAdditionalFriendField").appendTo("#buttoncell")
          $("#newField").removeAttr("id")
          $("#buttoncell").removeAttr("id")
          $("#textcell").removeAttr("id")
          });
          
     setPopover("addFriend", "addFriendForm");
     setPopover("friendRequests", "friendRequestForm");
     setPopover("addSymbol", "addSymbolForm");
     setPopover("removeSymbol", "removeSymbolForm");
     setPopover("tradeMenu", "tradeForm");
     setPopover("inviteFriend", "inviteForm");
     
     $('.analysisCriteria').change(repopulateTable() );
          
     if ($("#param").html() != "") {
          $("#stockTextBox").val($("#param").html())
          repopulateTable()
     }
}

function repopulateTable() {
     document.getElementById('histpic').src = 'transparent.png';
     var textValue = $("#stockTextBox").val()
     
     if (textValue.length === 0) {
          $('#histpic').attr("src", "/assets/transparent.png");
          $('#stocktable td').html("")
          return; 
     }
     
     var script = document.createElement('script');
     
     script.src = 'http://finance.yahoo.com/webservice/v1/symbols/' +
          textValue + '/quote?format=json&callback=myCallBack';

     document.body.appendChild(script);
     
     var tval = document.getElementById('length');
     var tlength = tval.options[tval.selectedIndex].value;
     var n = parseInt(document.getElementById('timepd').value) || 0;
     var dur = '';
     
     if((n > 0 && tlength != 'blank') || tlength == 'alltime'){
          if(tlength == "days"){
               dur = '&t=' + n + 'd';
          }
          else if(tlength == "months"){
               dur = '&t=' + n + 'm';
          }
          else if(tlength == "years"){
               dur = '&t=' + n + 'y';
          }
          else{
               dur = '&t=my'
          }
     }       
     $('#histpic').attr("src", 'http://chart.finance.yahoo.com/z?s=' + currentText() + dur); 

}

function setPopover(id, form) {
     var timeout = 20000
     $("#"+id).popover({html:true, content:function() {
               return $('#'+form).html()}
          }).click(function () {
               setTimeout(function () {
                    $('#'+id).popover('hide');
               }, timeout);
          });
}

$(function() {
     initPage();
});

$(window).bind('page:change', function() {
     initPage();
});

function currentText() {
    return $("#stockTextBox").val();
}

function myCallBack(data) {
    if (data.list.resources.length > 0) {
        var fields = data.list.resources[0].resource.fields;
        document.getElementById("name").innerHTML = fields.name;
        document.getElementById("price").innerHTML = "$" + fields.price;
        document.getElementById("symbol").innerHTML = fields.symbol;
        document.getElementById("typeofstock").innerHTML = fields.type;
        document.getElementById("volume").innerHTML = fields.volume;
    }
}

